<?php

namespace backend\models;

use Yii;
use common\models\UserInfo;

/**
 * This is the model class for table "log_user_score".
 *
 * @property integer $id
 * @property integer $uid
 * @property integer $score
 * @property string $score_key
 * @property integer $time
 * @property integer $state
 * @property string $memo
 */
class LogUserScore extends \yii\db\ActiveRecord
{
	const STATE_EXCHANGE = 100;
	const STATE_DESIGNER_PASS = 3;
	public function attributes()
    {
        // add related fields to searchable attributes
        return array_merge(parent::attributes(), ['tel']);
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'log_user_score';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'score', 'time', 'state'], 'integer'],
            [['memo'], 'string'],
            [['score_key'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'uid' => '用户ID',
            'tel' => '手机号码',
            'score' => '金币',
            'score_key' => 'Score Key',
            'time' => '申请时间',
            'state' => 'State',
            'memo' => '备注',
        ];
    }
    //积分兑换用户信息
    public function getUserInfo()
    {
    	return $this->hasOne(UserInfo::className(), ['id'=>'uid']);
    }
    public function deleteLog($id,$state){
    	$info = $this->findOne(['id'=>$id,'state=>$state']);
    	if($info !== null){
    		if($info->delete())
    			return true;
    	}
    	return false;
    }
    //注册VIP会员送积分
    function designerPoint($uid){
    	$config = PrfConfig::findOne(['config_attribute'=>'designer_register_point','description'=>'1']);
    	if ($config !== null) {
    		$value = $config->config_value;
    		if($value > 0){
    			//添加金币增加日志，只加一次
    			//$if_exist = self::findOne(['uid'=>$uid,'state'=>self::STATE_DESIGNER_PASS]);
    			//if($if_exist === null){
	    			$log_user_score = new LogUserScore();
	    			$log_user_score->uid = $uid;
	    			$log_user_score->score = $value;
	    			$log_user_score->score_key = date('Y-m-d');
	    			$log_user_score->time = time();
	    			$log_user_score->state = 3;
	    			$log_user_score->memo = $config->config_name;
	    			$log_user_score->save();
    			//}
    		}
    	}
    	 
    }
}
